package com.dongdongshop.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.dk.util.MyUtil;
import com.dongdongshop.mapper.TbContentMapper;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategoryExample;
import com.dongdongshop.pojo.TbContentExample;
import com.dongdongshop.service.TbcontentService;
import com.google.gson.Gson;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

//广告管理
@Service
@DubboService
public class TbcontentServiceimpl implements TbcontentService {

    @Autowired
    TbContentMapper tcm;

    @Autowired
    RedisTemplate redisTemplate;


    //查询全部
    @Override
    public List<TbContent> getall() {
        return tcm.selectByExample(null);
    }

    //增加
    @Override
    public int addcont(TbContent tc) {
        return tcm.insertSelective(tc);
    }


    //修改回显
    @Override
    public TbContent upbyid(Long id) {
        return tcm.selectByPrimaryKey(id);
    }

    //修改
    @Override
    public int upcont(TbContent tc) {
        return tcm.updateByPrimaryKeySelective(tc);
    }

    //删除
    @Override
    public int deletearr(List<Long> arrs) {
        TbContentExample te = new TbContentExample();
        TbContentExample.Criteria criteria = te.createCriteria();
        criteria.andIdIn(arrs);
        return tcm.deleteByExample(te);
    }


    //查询轮播图
    @Override
    public List<TbContent> getlun(Long id) {
        Set<String> lunlist = redisTemplate.boundSetOps("lunbo").members();
        if (lunlist.size() != 0) {
            String s = lunlist.toString();
            List<TbContent> tbContents = JSONObject.parseArray(s, TbContent.class);
            return tbContents;
        } else {
            List<TbContent> ls = tcm.bycateid(id);
            for (int i = 0; i < ls.size(); i++) {
                String s = new Gson().toJson(ls.get(i));
                redisTemplate.boundSetOps("lunbo").add(s);
            }
            return ls;
        }
    }

    //今日推荐
    @Override
    public List<TbContent> gettui(Long id) {
        String key="tuijian"+id;
        Set<String> tuilist = redisTemplate.boundSetOps(key).distinctRandomMembers(4l);
        if(tuilist.size()!=0){
            String s = tuilist.toString();
            //将json格式的字符串转换为list集合
            List<TbContent> tbContents = JSONObject.parseArray(s, TbContent.class);
            return tbContents;
        }else{
//            List<TbContent> ls = tcm.bycateid(id);
            List<TbContent> ls = tcm.tuijianid(id);
            for(int i=0;i<ls.size();i++){
                redisTemplate.boundSetOps(key).add(new Gson().toJson(ls.get(i)));
            }
            return ls;
        }

    }

    //猜你喜欢
    @Override
    public List<TbContent> getlike(Long id) {
        String key="like"+id;
        Set<String> likels = redisTemplate.boundSetOps(key).distinctRandomMembers(6l);//随机取6ge
       if(likels.size()!=0){
           String s = likels.toString();
           List<TbContent> tbContents = JSONObject.parseArray(s, TbContent.class);
           return tbContents;
       }else{
           List<TbContent> lsi = tcm.bycateid(id);//全部
           List<TbContent> ls = tcm.bylike(id);//6tiao
           for(int i=0;i<lsi.size();i++){
               redisTemplate.boundSetOps(key).add(new Gson().toJson(lsi.get(i)));
           }
           return ls;
       }
    }
}
